package com.ruoyi.house.strategy;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 数据脱敏工具类
 */
public class DataMaskUtil {

    /**
     * 处理对象脱敏
     * @param object 需要脱敏的对象
     */
    public static void mask(Object object) throws IllegalAccessException {
        if (object == null) {
            return;
        }

        Class<?> clazz = object.getClass();
        Field[] fields = clazz.getDeclaredFields();

        for (Field field : fields) {
            if (field.isAnnotationPresent(DataMask.class)) {
                field.setAccessible(true);
                DataMask annotation = field.getAnnotation(DataMask.class);
                Object value = field.get(object);

                if (value != null) {
                    String maskedValue = maskValue(String.valueOf(value), annotation);
                    field.set(object, maskedValue);
                }
            }
        }
    }

    /**
     * 处理列表脱敏
     * @param list 需要脱敏的对象列表
     */
    public static <T> void maskList(List<T> list) throws IllegalAccessException {
        if (list == null || list.isEmpty()) {
            return;
        }

        for (T item : list) {
            mask(item);
        }
    }

    private static String maskValue(String value, DataMask annotation) {
        switch (annotation.value()) {
            case APARTMENT_ID:
                return maskString(value, 0, 2, annotation.symbol());
            case CHINESE_NAME:
                return maskChineseName(value, annotation.symbol());
            case PHONE:
                return maskString(value, 3, 4, annotation.symbol());
            case DATE:
                return maskDate(value);
            case CUSTOM:
                return maskString(value, annotation.prefix(), annotation.suffix(), annotation.symbol());
            default:
                return maskString(value, 2, 2, annotation.symbol());
        }
    }

    private static String maskString(String str, int prefixLen, int suffixLen, char symbol) {
        if (str == null || str.length() <= prefixLen + suffixLen) {
            return str;
        }

        String prefix = str.substring(0, prefixLen);
        String suffix = suffixLen > 0 ? str.substring(str.length() - suffixLen) : "";
        String middle = new String(new char[str.length() - prefixLen - suffixLen]).replace('\0', symbol);

        return prefix + middle + suffix;
    }

    private static String maskChineseName(String name, char symbol) {
        if (name == null || name.isEmpty()) {
            return name;
        }
        return name.charAt(0) + new String(new char[name.length() - 1]).replace('\0', symbol);
    }

    private static String maskDate(String dateStr) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date = sdf.parse(dateStr);
            return new SimpleDateFormat("yyyy").format(date) + "-**-**";
        } catch (Exception e) {
            return dateStr;
        }
    }
}